// pages/list.js
const product = require('../../utils/product.js')
var app = getApp()
Page({
    data: {
        primarySize: '45',
        receiverName: "",
        receiverMobile: "",
        receiverProvince: "",
        receiverAddress: "",
        num: '',
        sumPrice: '',
        orderid:'',
    },

    onLoad: function (obj) {
        var that = this
        var num = getApp().data.info.buyNumber
        var sumPrice = getApp().data.info.price * num
        this.setData({ num: num })
        this.setData({ sumPrice: sumPrice })
        console.log("options222", obj);
        product.getinfo(obj.id, function (resolve) {
            var data = resolve.data;
            console.log("info222", data);
            that.setData({ 'item': data })
            wx.setStorage({
                key: "detail",
                data: data
            })
        })
    },



    formSubmit: function (e) {
        var that=this;
        //进行input的前端数据验证，并提交数据至服务器
        var receiverProvince = e.detail.value.inputProvince.trim()
        var receiverName = e.detail.value.inputName.trim()
        var receiverMobile = e.detail.value.inputMobile.trim()
        var receiverAddress = e.detail.value.inputAddress.trim()
        if (!(receiverName && receiverMobile && receiverProvince && receiverAddress)) {
            wx.showModal({ title: '提示', content: '填写内容不能为空', })
            return
        }
        if (!receiverMobile.match(/^1[3-9][0-9]\d{8}$/)) {
            wx.showModal({ title: '提示', content: '手机号格式不正确，仅支持国内手机号码', })
            return
        }
        this.setData({ receiverProvince: receiverProvince })
        this.setData({ receiverName: receiverName })
        this.setData({ receiverMobile: receiverMobile })
        this.setData({ receiverAddress: receiverAddress })
        console.log('form发生了submit事件，携带数据为88888888：', e.detail.value)
        var res = e.detail.value
         var session_3rd = wx.getStorageSync('session_3rd')
        wx.request({
            url: `${app.globalData.API_URL}/order`,
            data: {
                product_id:that.data.item.id,
                product_name:that.data.item.cat_title,
                product_price:that.data.item.price,
                product_sku:that.data.sumPrice,
                product_count:that.data.num,
                receiver_address: res.inputAddress,
                receiver_mobile: res.inputMobile,
                receiver_name: res.inputName,
                receiver_province: res.inputProvince,
                session_3rd:session_3rd,
                mp_id: getApp().data.mp_id
            },

            
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            method: 'post',
            success: function (res) {
                console.log(res.data.order_id)
                var orderid=res.data.order_id
                wx.navigateTo({
                    url: `../payorder/payorder?orderid=`+orderid
                })
                console.log("GGGGGGGGGGGGG", res.data)
                wx.setStorage({
                    key: "order",
                    data: res.data
                })
                try {
                    wx.setStorageSync('infodata', res.data)
                } catch (e) {
                }
            },
            fail: function () {
                // fail
            },
            complete: function () {
                // complete
            }
        })
    },
})
